@echo off
chcp 65001 >nul
echo ====================================
echo   Testing Enhanced Restart Script
echo ====================================
echo.

REM Test 1: Check if restart.bat exists
echo [TEST 1] Checking if restart.bat exists...
if not exist "restart.bat" (
    echo ❌ FAIL: restart.bat not found
    pause
    exit /b 1
)
echo ✅ PASS: restart.bat found

REM Test 2: Check Python availability
echo.
echo [TEST 2] Checking Python installation...
python --version >nul 2>&1
if errorlevel 1 (
    echo ❌ FAIL: Python not available
    echo Please install Python first
    pause
    exit /b 1
)
python --version > temp_version.txt 2>&1
set /p PYTHON_VERSION=<temp_version.txt
del temp_version.txt >nul 2>&1
echo ✅ PASS: Python available

REM Test 3: Check Node.js availability
echo.
echo [TEST 3] Checking Node.js installation...
node --version >nul 2>&1
if errorlevel 1 (
    echo ❌ FAIL: Node.js not available
    echo Please install Node.js first
    pause
    exit /b 1
)
node --version > temp_node_version.txt 2>&1
set /p NODE_VERSION=<temp_node_version.txt
del temp_node_version.txt >nul 2>&1
echo ✅ PASS: Node.js available

REM Test 4: Check project structure
echo.
echo [TEST 4] Checking project structure...
if not exist "backend" (
    echo ❌ FAIL: backend directory not found
    pause
    exit /b 1
)
if not exist "frontend" (
    echo ❌ FAIL: frontend directory not found
    pause
    exit /b 1
)
if not exist "backend\requirements.txt" (
    echo ❌ FAIL: backend\requirements.txt not found
    pause
    exit /b 1
)
if not exist "frontend\package.json" (
    echo ❌ FAIL: frontend\package.json not found
    pause
    exit /b 1
)
echo ✅ PASS: Project structure is correct

REM Test 5: Check if ports are available
echo.
echo [TEST 5] Checking port availability...
netstat -an | findstr ":8000" >nul 2>&1
if not errorlevel 1 (
    echo ⚠️  WARNING: Port 8000 is in use (will be handled by restart.bat)
) else (
    echo ✅ PASS: Port 8000 is available
)

netstat -an | findstr ":3000" >nul 2>&1
if not errorlevel 1 (
    echo ⚠️  WARNING: Port 3000 is in use (will be handled by restart.bat)
) else (
    echo ✅ PASS: Port 3000 is available
)

REM Test 6: Check virtual environment
echo.
echo [TEST 6] Checking virtual environment status...
if exist "venv" (
    echo ✅ INFO: Virtual environment already exists
    if exist "venv\Scripts\activate.bat" (
        echo ✅ PASS: Virtual environment is properly configured
    ) else (
        echo ❌ FAIL: Virtual environment is corrupted
        echo Recommendation: Delete venv folder and run restart.bat
    )
) else (
    echo ✅ INFO: Virtual environment will be created by restart.bat
)

echo.
echo ====================================
echo        Test Results Summary
echo ====================================
echo.
echo ✅ All prerequisite checks passed!
echo.
echo 🚀 Ready to run enhanced restart.bat
echo.
echo 💡 What the enhanced script will do:
echo   1. Create virtual environment (if not exists)
echo   2. Activate virtual environment
echo   3. Install/update Python dependencies
echo   4. Install/update Node.js dependencies
echo   5. Check and free up ports if needed
echo   6. Start backend server with virtual environment
echo   7. Start frontend development server
echo   8. Verify both services are running
echo   9. Open browser automatically
echo.
echo Press any key to run the enhanced restart.bat...
pause >nul

echo.
echo [INFO] Launching enhanced restart.bat...
call restart.bat
